package OnlineExam.question;

import javax.servlet.*;
import javax.servlet.http.*;
import org.apache.struts.action.*;
import OnlineExam.db.*;
import OnlineExam.*;

/**
 *  Adds a question to the question bank.
 */


public class AddQuestionAction extends Action
{

	/**
	 *  An Action servlet to add a question to the QuestionBank.
	 */
		
	public ActionForward execute(ActionMapping mapping, ActionForm form, HttpServletRequest request, 
					HttpServletResponse response)
	{

		HttpSession sess = request.getSession ();
		Category cat = (Category) sess.getAttribute ("CATEGORY");
		//
		//gets the options from the form and concates a separator to it.
		//
		Question q = (Question) form;			
		String optionsString = Question.assembleOptions (q.getOptionsArr());
		q.setCategoryId (cat.getCategoryId());
		q.setOptions (optionsString);
		if (q.getExplanation() == null)
				q.setExplanation("N.A");

    	try
		{
			DBUtil.insert (q);
			return (mapping.findForward("success"));
		} 
		catch (DBException e) 
		{
			System.out.println (e.getMessage());
			return (mapping.findForward("dbError"));
		}
	}
}
